#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'classes/TestRecord.php';
require_once 'classes/TestRelatedRecord.php';

// set up the table(s)
AIR2_DBManager::init();
// clean up table from any prior aborted run
$conn = AIR2_DBManager::get_master_connection();
$conn->execute('drop table if exists test_related');
$conn->execute('drop table if exists test');
// create new table
Doctrine::createTablesFromArray(array('TestRecord', 'TestRelatedRecord'));


// init browser
$browser = new AirHttpTest();
$browser->set_user('testuser', array());

// set to json
$browser->set_content_type(AirHttpTest::$JSON);


plan(19);

/**********************
 * create new record
 */
$test_record = new TestRecord();
$test_record->test_string = 'my test string';
$test_record->save();

/**********************
 * read
 */
ok( $resp = $browser->http_get('/test_crud/'.$test_record->test_uuid),
    "GET new thing");
ok( $thing = json_decode($resp), "JSON decode GET");
is( $thing->success, true, "GET success" );

// check that cre_dtim was set but NOT upd_dtim
ok( $thing->radix->test_cre_dtim, "cre_dtim set" );
is( $thing->radix->test_upd_dtim, $thing->radix->test_cre_dtim,
    "upd_dtim equal to cre_dtim" );

// check that test_cre_user and test_upd_user were unset
ok( !isset($thing->radix->test_cre_user), "test_cre_user unset" );
ok( !isset($thing->radix->test_upd_user), "test_upd_user unset" );

// check that CreUser and UpdUser objects were created, without ID's set
ok( isset($thing->radix->CreUser), "CreUser object created" );
ok( $thing->radix->CreUser, "CreUser object not null" );
is( $thing->radix->UpdUser, $thing->radix->UpdUser,
    "UpdUser object equal to CreUser" );

// check unsets
$uuid_col = 'user_uuid';
$usernm_col = 'user_username';
$userid_col = 'user_id';
ok( isset($thing->radix->CreUser->$uuid_col), "CreUser uuid set");
ok( isset($thing->radix->CreUser->$usernm_col), "CreUser username set");
ok( !isset($thing->radix->CreUser->$userid_col), "CreUser user_id unset");

// update!
$cre_timestamp = $thing->radix->test_cre_dtim;
$creuseruuid = $thing->radix->CreUser->$uuid_col;
$creusername = $thing->radix->CreUser->$usernm_col;
$test_record->test_string = 'newer string here 2';
$test_record->save();

/**********************
 * read -- again
 */
ok( $resp = $browser->http_get('/test_crud/'.$test_record->test_uuid),
    "GET updated thing");
ok( $thing = json_decode($resp), "JSON decode GET updated");
is( $thing->success, true, "GET updated success" );

// check that UpdUser and test_upd_dtim were set
ok( $thing->radix->UpdUser, "UpdUser object not null" );
ok( $thing->radix->test_upd_dtim, "upd_dtim set" );

// check that upd_dtim is after cre_dtim
ok( $thing->radix->test_upd_dtim >= $cre_timestamp, "update after create ts" );



// clean up tables
$conn->execute('drop table test_related');
$conn->execute('drop table test');
?>
